<script>
export default {
  data() {
    return {
      institution:{
        id: '',
        organization: '',
        principalname: '',
        phone: '',
        address: '',
        introduce: '',
        img:''
      },
      imageUrl:'',
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.institution.img=res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    submitForm(){
      this.axios.post(`http://localhost:8302/institution/save`,this.institution).then(res=>{
        this.fh()
        this.$message({
          type: 'success',
          message: '保存成功!'
        })
      })
    },
    fh(){
      this.imageUrl=''
      this.institution={}
      this.$router.push('/jg');
    },
    findinstitution(){
      var a=JSON.parse(localStorage.getItem("institution")).id
      this.axios.get(`http://localhost:8302/institution/queryById?id=${a}`).then(res=>{
        this.institution=res.data.data;
        this.imageUrl=this.institution.img
      })
      localStorage.removeItem("institution")
    }
  },
  created(){
    // this.submitForm();
    this.findinstitution();
  }
}
</script>

<template>
  <div style="width: 100%;margin-left: 40px;margin-right: 40px" align="center;">
    <div style="background-color: #F2F7FB;padding: 10px 0px 0px 0px" >
      <el-header align="left" style="margin-top: 60px;"> |  新增机构信息</el-header>
      <el-main>
        <el-form label-width="120px">
<!--          <el-form-item label="机构编号">-->
<!--            <el-input v-model="institution.id" disabled></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="机构名称">
            <el-input v-model="institution.organization" required></el-input>
          </el-form-item>
          <el-form-item label="机构图片">
            <el-upload
                align="left"
                class="avatar-uploader"
                action="http://localhost:8302/bao/tp"
                name="file"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img width="50px" v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="负责人姓名">
            <el-input v-model="institution.principalname"></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="institution.phone"></el-input>
          </el-form-item>
          <el-form-item label="机构地址">
            <el-input v-model="institution.address"></el-input>
          </el-form-item>
          <el-form-item label="机构介绍">
            <el-input type="textarea" v-model="institution.introduce"></el-input>
          </el-form-item>
        </el-form>
        <el-button style="width: 100px;" type="primary" @click="submitForm">提交</el-button>
        <el-button style="width: 100px;" @click="fh">返回</el-button>
      </el-main>
    </div>
  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
